﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace XWord.WPF.ApplicationFramework
{
    public static class ApplicationExtension
    {

        public static void OpenMainWindow(this Application app)
        {
            Application.Current.MainWindow.WindowState = WindowState.Normal;
        }

        public static void DirectClose(this Application app)
        {
           
        }

        public static string GetAppPath(this Application app)
        {
            return AppDomain.CurrentDomain.BaseDirectory + AppDomain.CurrentDomain.FriendlyName;
        }

        public static void SetAutoRun(this Application app,bool bAutoRun)
        {
            RegistryKey currentUser = Registry.CurrentUser;
            string name = @"Software\Microsoft\Windows\CurrentVersion\Run";
            string appName = "XWord.GUI.exe";
            try
            {
                currentUser = currentUser.OpenSubKey(name, true);
                if (currentUser == null)
                {
                    return;
                }
            }
            catch (Exception)
            {
                return;
            }
            try
            {
                if (bAutoRun)
                {
                    name = GetAppPath(app);
                    name = "\"" + name + "\" -AutoStart";
                    currentUser.SetValue(appName, name, RegistryValueKind.String);
                }
                else if (currentUser.GetValue(appName) != null)
                {
                    currentUser.DeleteValue(appName);
                }
                currentUser.Close();
            }
            catch (Exception exception)
            {
                
            }
        }

        public static bool IsAutoRun(this Application app)
        {
            return IsAutoRun(app, "XWord.GUI.exe");
        }

        public static bool IsAutoRun(this Application app,string appName)
        {
            RegistryKey currentUser = Registry.CurrentUser;
            string name = @"Software\Microsoft\Windows\CurrentVersion\Run";
            try
            {
                currentUser = currentUser.OpenSubKey(name, true);
                if (currentUser == null)
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
            name = currentUser.GetValue(appName) as string;
            currentUser.Close();
            if (string.IsNullOrEmpty(name))
            {
                return false;
            }
            return true;
        }

        public static void ForceGC(this Application app)
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
            }
            GC.Collect();
        }

        [DllImport("kernel32.dll")]
        private static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max);
    }
}
